include( "shared.lua" )
require( "mysql" )
require( "datastream" )


StoreTools = {}
StoreAbilities = {}
StoreSENTs = {}
StoreSENTNAMES = {}
StoreFLAGS = {}
StoreAccessories = {}
StoreAccModels = {}
AccessSizes = {}
AccessOffsets = {}
AccessRotation = {}

StoreItemInfoS = {}
StoreItemCostS = {}


//TODO: Add the Empty Inventory and Inventory loading messages to the Bag.
//Add the Owner menu for manipulating and/or adding items to the shop.
//MAYBE make a wall jump ability for the inventory
//Add the rest of the Access items afterwards, including editing the entity to change it's bone depending on the type of access. Make Shoes spawn at each foot, hands do the same.

if SERVER then

function RebuildStoreTables()
	local DBTools = StoreDBINFO( "storetools" )
	local DBAbilities = StoreDBINFO( "storeabilities" )
	local DBSents = StoreDBINFO( "storesents" )
	local DBFlags = StoreDBINFO( "storeflags" )
	local DBAcc = StoreDBINFO( "storeaccessories" )
	
	print( "Tables Rebuilt" )
	
	//Tools here
	for k,v in pairs( DBTools ) do
		StoreTools[k] = DBTools[k][2]
	end

	for k,v in pairs( StoreTools ) do
		StoreItemInfoS[v] = DBTools[k][3]
	end
	for k,v in pairs( StoreTools ) do
		StoreItemCostS[v] = DBTools[k][4]
	end

	
	//Abilities here
	for k,v in pairs( DBAbilities ) do
		StoreAbilities[k] = DBAbilities[k][2]
	end
	
	for k,v in pairs( StoreAbilities ) do
		StoreItemInfoS[v] = DBAbilities[k][3]
	end
	for k,v in pairs( StoreAbilities ) do
		StoreItemCostS[v] = DBAbilities[k][4]
	end
	
	//SENTs here
	for k,v in pairs( DBSents ) do
		StoreSENTs[k] = DBSents[k][2]
	end
	
	for k,v in pairs( StoreSENTs ) do
		StoreSENTNAMES[k] = DBSents[k][3]
	end
	for k,v in pairs( StoreSENTs ) do
		StoreItemInfoS[v] = DBSents[k][4]
	end
	for k,v in pairs( StoreSENTs ) do
		StoreItemCostS[v] = DBSents[k][5]
	end
	
	//FLAGS here
	for k,v in pairs( DBFlags ) do
		StoreFLAGS[k] = DBFlags[k][2]
	end

	for k,v in pairs( StoreFLAGS ) do
		StoreItemInfoS[v] = DBFlags[k][3]
	end
	for k,v in pairs( StoreFLAGS ) do
		StoreItemCostS[v] = DBFlags[k][4]
	end
	
	//Accessories here
	for k,v in pairs( DBAcc ) do
		StoreAccessories[k] = DBAcc[k][2]
	end
	
	for k,v in pairs( StoreAccessories ) do
		StoreAccModels[v] = DBAcc[k][3]
	end
	for k,v in pairs( StoreAccessories ) do
		local vx, vy, vz = 0
		for w in string.gmatch( DBAcc[k][4], "x=.-%," ) do
			local ToX = string.Replace( w, "x=", "" )
			ToX = string.Replace( ToX, ",", "" )
			vx = tonumber(ToX)
		end
		for w in string.gmatch( DBAcc[k][4], "y=.-%," ) do
			local ToY = string.Replace( w, "y=", "" )
			ToY = string.Replace( ToY, ",", "" )
			vy = tonumber(ToY)
		end
		for w in string.gmatch( DBAcc[k][4], "z=.*" ) do
			local ToZ = string.Replace( w, "z=", "" )
			ToZ = string.Replace( ToZ, ",", "" )
			vz = tonumber(ToZ)
		end
		AccessSizes[v] = Vector( vx, vy, vz )
	end
	for k,v in pairs( StoreAccessories ) do
		local vx, vy, vz = 0
		for w in string.gmatch( DBAcc[k][5], "x=.-%," ) do
			local ToX = string.Replace( w, "x=", "" )
			ToX = string.Replace( ToX, ",", "" )
			vx = tonumber(ToX)
		end
		for w in string.gmatch( DBAcc[k][5], "y=.-%," ) do
			local ToY = string.Replace( w, "y=", "" )
			ToY = string.Replace( ToY, ",", "" )
			vy = tonumber(ToY)
		end
		for w in string.gmatch( DBAcc[k][5], "z=.*" ) do
			local ToZ = string.Replace( w, "z=", "" )
			ToZ = string.Replace( ToZ, ",", "" )
			vz = tonumber(ToZ)
		end
		AccessOffsets[v] = Vector( vx, vy, vz )
	end
	for k,v in pairs( StoreAccessories ) do
		local ap, ay, ar = 0
		for w in string.gmatch( DBAcc[k][6], "p=.-%," ) do
			local ToP = string.Replace( w, "p=", "" )
			ToP = string.Replace( ToP, ",", "" )
			ap = tonumber(ToP)
		end
		for w in string.gmatch( DBAcc[k][6], "y=.-%," ) do
			local ToY = string.Replace( w, "y=", "" )
			ToY = string.Replace( ToY, ",", "" )
			ay = tonumber(ToY)
		end
		for w in string.gmatch( DBAcc[k][6], "r=.*" ) do
			local ToR = string.Replace( w, "r=", "" )
			ToR = string.Replace( ToR, ",", "" )
			ar = tonumber(ToR)
		end
		AccessRotation[v] = Angle( ap, ay, ar )
		print( Angle( ap, ay, ar ) )
	end
	for k,v in pairs( StoreAccessories ) do
		StoreItemInfoS[v] = DBAcc[k][7]
	end
	for k,v in pairs( StoreAccessories ) do
		StoreItemCostS[v] = DBAcc[k][8]
	end
	
	local DATA = { StoreTools, StoreAbilities, StoreSENTs, StoreSENTNAMES, StoreFLAGS, StoreAccessories, StoreAccModels, AccessSizes, AccessOffsets, AccessRotation, StoreItemInfoS, StoreItemCostS }
	datastream.StreamToClients( player.GetAll(), "StoreStream", DATA )
	PrintMessage( HUD_PRINTTALK, "Receiving Store Information." )
	
	for pn,p in pairs( player.GetAll() ) do
		p:SetNWBool( "CanF2", false )
	end
	
end

function StreamStore( ply )

	local DATA = { StoreTools, StoreAbilities, StoreSENTs, StoreSENTNAMES, StoreFLAGS, StoreAccessories, StoreAccModels, AccessSizes, AccessOffsets, AccessRotation, StoreItemInfoS, StoreItemCostS }
	datastream.StreamToClients( { ply }, "StoreStream", DATA )
	ply:PrintMessage( HUD_PRINTTALK, "Receiving Store Information." )
	
end
hook.Add("PlayerInitialSpawn", "StreamStore", StreamStore)

end

if CLIENT then

function StoreStreamHook( pl, handle, id, encoded, decoded )

	StoreTools = encoded[1]
	StoreAbilities = encoded[2]
	StoreSENTs = encoded[3]
	StoreSENTNAMES = encoded[4]
	StoreFLAGS = encoded[5]
	StoreAccessories = encoded[6]
	StoreAccModels = encoded[7]
	AccessSizes = encoded[8]
	AccessOffsets = encoded[9]
	AccessRotation = encoded[10]

	StoreItemInfoS = encoded[11]
	StoreItemCostS = encoded[12]
end
datastream.Hook( "StoreStream", StoreStreamHook )

end


AllowedSWEPS = {}
AllowedSWEPS[1] = "weapon_physcannon"
AllowedSWEPS[2] = "weapon_physgun"
AllowedSWEPS[3] = "gmod_tool"
AllowedSWEPS[4] = "gmod_camera"
AllowedSWEPS[5] = "laserpointer"


AccessTypes = {}
AccessTypes[1] = "HAT"
AccessTypes[2] = "HEAD"
AccessTypes[3] = "FACE"
AccessTypes[4] = "SHOES"
AccessTypes[5] = "ARMS"
AccessTypes[6] = "HANDS"

DataBases = {}
DataBases[1] = "Player Info Database"
DataBases[2] = "Ban Database"
DataBases[3] = "Store Database"



//USE THIS FOR DATASTREAMS
if SERVER then
	local META = FindMetaTable("CRecipientFilter")  
	function META:IsValid()  
		return true  
	end
	META = nil
end